﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using NPMonitor.Utility;
using NPMonitor.Attributes;
using NPMonitor.DataModel;
using NPMonitor.Utility;

namespace SMSLogin
{
    public class SMSJob_Right : WebJobBase
    {
        protected override void ExecuteJob(HttpWatch.Plugin plugin)
        {
            Console.WriteLine("Starting SMSJob --");

            try
            {
                WebDriverHelper.WebDriver = WebDriver;
                WebDriverHelper.DefaultTimeoutSecs = DefaultTimeoutSecs;

                WebDriver.Navigate().GoToUrl("http://ws-dc-01:8082");
                //最大化窗口
                WebDriver.Manage().Window.Maximize();

                //try
                //{
                    //Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);

                    SLogin(WebDriver, plugin);

                    //Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);

                    ScearioAddNewContact(WebDriver, plugin);

                }
                catch (Exception ex)
                {
                    ExecutionResult.ErrorMessage = ex.Message;
                    if (ex.InnerException != null)
                    {
                        ExecutionResult.ErrorMessage = ex.Message + " -----> " + ex.InnerException.Message;
                    }
                    ExecutionResult.ResultStatus = JobResultStatus.Fail;
                }
                //}
                //catch (Exception ex)
                //{
                //    ExecutionResult.ErrorMessage = ex.Message;
                //}
                //finally
                //{
                //    if (!string.IsNullOrWhiteSpace(errorMessage))
                //    {
                //        ExecutionResult.ErrorMessage = errorMessage;
                //        ExecutionResult.ResultStatus = JobResultStatus.Fail;
                //    }
                //    ExecutionResult.ErrorMessage = ex.Message;
                //    ExecutionResult.ResultStatus = JobResultStatus.Fail;
                //}
            Console.WriteLine("SMSJob -- " + " Finished");
            return;
        }

        [Scenario("RightScenario001", "Login")]
        public void SLogin(IWebDriver web, HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Scenario001";
            var InputUserName = WebDriverHelper.GetById(WebDriver, "InputUserName");//web.FindElement(By.Id("InputUserName"));

            InputUserName.Clear();

            InputUserName.SendKeys("dada liao");

            var InputPassword = WebDriverHelper.GetById(WebDriver, "InputPassword");//web.FindElement(By.Id("InputPassword"));

            InputPassword.Clear();

            InputPassword.SendKeys("11");

            var submitButton = WebDriverHelper.GetById(WebDriver, "SubmitButton");//web.FindElement(By.Id("SubmitButton"));

            submitButton.Click();
        }

        [Scenario("RightScenario002","Select contact")]
        public void ScearioAddNewContact(IWebDriver web, HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Scenario002";
            var address_CSS = WebDriverHelper.GetById(WebDriver, "address_CSS");//web.FindElement(By.Id("address_CSS"));

            address_CSS.Click();

            //Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);

            var add = WebDriverHelper.GetByName(WebDriver, "add");//web.FindElement(By.Name("add"));

            add.Click();

            //Control.WaitEx(plugin, DefaultTimeoutSecs, false, false, 0);

            string fakeName = Faker.Name.FullName();
            string fakenumber = Faker.Phone.Number();

            var newname = WebDriverHelper.GetById(WebDriver, "newname");//web.FindElement(By.Id("newname"));
            newname.Clear();
            newname.SendKeys(fakeName);

            var newnum = WebDriverHelper.GetById(WebDriver, "newnum");//web.FindElement(By.Id("newnum"));
            newnum.Clear();
            newnum.SendKeys(fakenumber.Substring(0, 10));

            var ok = WebDriverHelper.GetByName(WebDriver, "ok");//web.FindElement(By.Name("ok"));
            ok.Click();

            //Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0); ;

            var key = WebDriverHelper.GetByName(WebDriver, "key");//web.FindElement(By.Name("key"));
            key.Clear();
            key.SendKeys(fakeName);

            var searchbutton = WebDriverHelper.GetByClassName(WebDriver, "searchbutton");//web.FindElement(By.ClassName("searchbutton"));
            searchbutton.Click();

            //Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
        }


    }
}
